﻿using Analyzer.Properties;
using Product;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UboxUI;

namespace Analyzer
{
    public partial class FrmAnalyzerList : FrmFather
    {
        public FrmAnalyzerList()
        {
            InitializeComponent();
            this.LblTitle.Text = this.Text;
            this.LblTitle2.Text = LblTitle.Text;
            this.BtnClose.Left = this.Width - this.BtnClose.Width - 10;
            this.BtnNarrow.Left = this.BtnClose.Left - this.BtnNarrow.Width - 10;
            this.panel1.Top = 0;
            this.panel2.Top = this.panel1.Height;
        }

        string conn = Settings.Default.strConn;

        //List<byte[]> listImageByte1 = new List<byte[]>();
        //List<byte[]> listImageByte2 = new List<byte[]>();
        //List<byte[]> listImageByte3 = new List<byte[]>();

        private void picProduct_Click(object sender, EventArgs e)
        {
            
            int whichClick = int.Parse((sender as PictureBox).Tag.ToString());
            FrmProductsControl f = new FrmProductsControl();
            DialogResult result = f.ShowDialog();
            
            int proID = FrmProductsControl.proID;
            if(result == System.Windows.Forms.DialogResult.OK)
            {
                UboxDataClassesDataContext db = new UboxDataClassesDataContext(conn);

                var q = (from p in db.production_product
                         where p.product_id == proID && p.camera_resolution_rear_id == p.production_camera.camera_id
                         select new
                         {
                             proName = p.product_name,
                             proPrice = p.price,
                             proColor = p.color,
                             proSize = p.size,
                             proWeight = p.weight,
                             proBattery = p.battery_capacity,
                             proTalkTime = p.battery_talk_time,
                             proCategory = p.category.category_name,
                             proBrand = p.brand.brand_name,
                             proRom = p.production_rom.rom,
                             proRam = p.production_ram.ram,
                             proOS = p.production_operating_system.operating_system,
                             proConn = p.production_connection_standard.connection_standard,
                             proCore = p.production_processor_core.processor_core,
                             proSpeed = p.production_processor_speed.processor_speed,
                             proScreenSize = p.production_screen_size.screen_size,
                             proScreenR = p.production_srceen_resolution.screen_resolution,
                             //proRear
                             proFront = p.production_camera.camera_resolution,
                         }).First();

                var qImage = (from i in db.production_image
                              where i.product_id == proID
                              select i.image).First();

                byte[] image = qImage.ToArray();
                MemoryStream ms = new MemoryStream(image);
                Bitmap bmp = new Bitmap(ms);






                switch (whichClick)
                {

                    case 1:
                        #region 1
                                        
                  //var qImage = from i in db.production_image
                  //             where i.product_id == proID
                  //             select i.image;

                  //for (int i = 0; i < qImage.ToList().Count; i++)
                  //{
                  //    byte[] image = qImage.ToList()[i].ToArray();
                  //    listImageByte1.Add(image);
                  //};
                        this.labtxtProductName_01.Text = q.proName;
                        this.labPrice_01.Text = ((int)q.proPrice).ToString();
                        this.labtxtColor_01.Text = q.proColor;
                        this.labtxtSize_01.Text = q.proSize;
                        this.labtxtWeight_01.Text = q.proWeight.ToString();
                        this.labtxtBatteryCapacity_01.Text = q.proBattery;
                        this.labtxtTalkTime_01.Text = q.proTalkTime.ToString();
                        this.labCategory_01.Text = q.proCategory;
                        this.labBrand_01.Text = q.proBrand;
                        this.labRom_01.Text = q.proRom.ToString();
                        this.labRam_01.Text = q.proRam.ToString();
                        this.labOS_01.Text = q.proOS;
                        this.labConnStandard_01.Text = q.proConn;
                        this.labCore_01.Text = q.proCore;
                        this.labSpeed_01.Text = q.proSpeed.ToString();
                        this.labScreenSize_01.Text = q.proScreenSize.ToString();
                        this.labScreenResolution_01.Text = q.proScreenR;
                        this.labRearCamera_01.Text = q.proFront.ToString();
                        this.picProduct_01.Image = bmp;
                        break;
                        #endregion
                    case 2:
                        #region 2
                        this.labtxtProductName_02.Text = q.proName;
                        this.labPrice_02.Text = ((int)q.proPrice).ToString();
                        this.labtxtColor_02.Text = q.proColor;
                        this.labtxtSize_02.Text = q.proSize;
                        this.labtxtWeight_02.Text = q.proWeight.ToString();
                        this.labtxtBatteryCapacity_02.Text = q.proBattery;
                        this.labtxtTalkTime_02.Text = q.proTalkTime.ToString();
                        this.labCategory_02.Text = q.proCategory;
                        this.labBrand_02.Text = q.proBrand;
                        this.labRom_02.Text = q.proRom.ToString();
                        this.labRam_02.Text = q.proRam.ToString();
                        this.labOS_02.Text = q.proOS;
                        this.labConnStandard_02.Text = q.proConn;
                        this.labCore_02.Text = q.proCore;
                        this.labSpeed_02.Text = q.proSpeed.ToString();
                        this.labScreenSize_02.Text = q.proScreenSize.ToString();
                        this.labScreenResolution_02.Text = q.proScreenR;
                        this.labRearCamera_02.Text = q.proFront.ToString();
                        this.picProduct_02.Image = bmp;
                        break;
                        #endregion
                    case 3:
                        #region 3
                        this.labtxtProductName_03.Text = q.proName;
                        this.labPrice_03.Text = ((int)q.proPrice).ToString();
                        this.labtxtColor_03.Text = q.proColor;
                        this.labtxtSize_03.Text = q.proSize;
                        this.labtxtWeight_03.Text = q.proWeight.ToString();
                        this.labtxtBatteryCapacity_03.Text = q.proBattery;
                        this.labtxtTalkTime_03.Text = q.proTalkTime.ToString();
                        this.labCategory_03.Text = q.proCategory;
                        this.labBrand_03.Text = q.proBrand;
                        this.labRom_03.Text = q.proRom.ToString();
                        this.labRam_03.Text = q.proRam.ToString();
                        this.labOS_03.Text = q.proOS;
                        this.labConnStandard_03.Text = q.proConn;
                        this.labCore_03.Text = q.proCore;
                        this.labSpeed_03.Text = q.proSpeed.ToString();
                        this.labScreenSize_03.Text = q.proScreenSize.ToString();
                        this.labScreenResolution_03.Text = q.proScreenR;
                        this.labRearCamera_03.Text = q.proFront.ToString();
                        this.picProduct_03.Image = bmp;
                        break;
                        #endregion

                                    
           
                }

                p1 = int.TryParse(this.labPrice_01.Text, out price1);
                p2 = int.TryParse(this.labPrice_02.Text, out price2);
                p3 = int.TryParse(this.labPrice_03.Text, out price3);
                TextChanged(sender, e);
            }
        }
        bool p1, p2, p3;
        int price1, price2, price3;
        private void TextChanged(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(this.labPrice_01, "");
            this.errorProvider1.SetError(this.labPrice_02, "");
            this.errorProvider1.SetError(this.labPrice_03, "");
            if (p1 && p2 && p3)
            {
                #region
                if (Math.Min(price3, Math.Min(price1, price2)) == price1)
                {
                    this.errorProvider1.SetError(this.labPrice_01, "123");
                    this.errorProvider1.SetError(this.labPrice_02, "");
                    this.errorProvider1.SetError(this.labPrice_03, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                else if (Math.Min(price3, Math.Min(price1, price2)) == price2)
                {
                    this.errorProvider1.SetError(this.labPrice_02, "123");
                    this.errorProvider1.SetError(this.labPrice_01, "");
                    this.errorProvider1.SetError(this.labPrice_03, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.labPrice_03, "123");
                    this.errorProvider1.SetError(this.labPrice_02, "");
                    this.errorProvider1.SetError(this.labPrice_01, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                #endregion
            }



            else if (p2 && p3)
            {
                #region
                if (Math.Min(price2, price3) == price2)
                {
                    this.errorProvider1.SetError(this.labPrice_02, "123");
                    this.errorProvider1.SetError(this.labPrice_01, "");
                    this.errorProvider1.SetError(this.labPrice_03, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.labPrice_03, "123");
                    this.errorProvider1.SetError(this.labPrice_02, "");
                    this.errorProvider1.SetError(this.labPrice_01, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                #endregion
            }
            else if (p3 && p1)
            {
                #region
                if (Math.Min(price1, price3) == price1)
                {
                    this.errorProvider1.SetError(this.labPrice_01, "123");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.labPrice_03, "123");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                #endregion
            }
            else if (p1 && p2)
            {
                #region
                if (Math.Min(price1, price2) == price1)
                {
                    this.errorProvider1.SetError(this.labPrice_01, "123");
                    this.errorProvider1.SetError(this.labPrice_02, "");
                    this.errorProvider1.SetError(this.labPrice_03, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                else
                {
                    this.errorProvider1.SetError(this.labPrice_02, "123");
                    this.errorProvider1.SetError(this.labPrice_01, "");
                    this.errorProvider1.SetError(this.labPrice_03, "");
                    try
                    {
                        this.errorProvider1.Icon = Resources.win;
                    }
                    catch
                    {
                        //...
                    }
                }
                #endregion
            }
            //this.errorProvider1.Icon.Size = 
        }

        //int i1 = 0, i2 = 0, i3 = 0;
        //private void timer1_Tick(object sender, EventArgs e)
        //{
        //    if (i1 < listImageByte1.Count)
        //    {
        //        MemoryStream ms = new MemoryStream(listImageByte1[i1]);
        //        Bitmap bmp = new Bitmap(ms);
        //        this.picProduct_01.Image = bmp;
        //    }
        //    else
        //    {
        //        i1 = 0;
        //    }

        //}

        //private void timer2_Tick(object sender, EventArgs e)
        //{
        //    if (i2 < listImageByte1.Count)
        //    {
        //        MemoryStream ms = new MemoryStream(listImageByte1[i2]);
        //        Bitmap bmp = new Bitmap(ms);
        //        this.picProduct_02.Image = bmp;
        //    }
        //    else
        //    {
        //        i2 = 0;
        //    }
        //}

        //private void timer3_Tick(object sender, EventArgs e)
        //{
        //    if (i3 < listImageByte1.Count)
        //    {
        //        MemoryStream ms = new MemoryStream(listImageByte1[i3]);
        //        Bitmap bmp = new Bitmap(ms);
        //        this.picProduct_03.Image = bmp;
        //    }
        //    else
        //    {
        //        i3 = 0;
        //    }
        //}

    }
}
